package com.web;

import java.util.HashMap;

public class ProductManageAction extends ActionSupport{
	
	protected IProductService productService;
	protected ICustomerService customerService;
	
	private Integer id;
	private String name;
	private String products;
	private String special;
	private Integer productId;
	private String customerId;
	private HashMap<String, Object> resultMap = new HashMap<String, Object>();
	
	
	public String manageProduct() throws Exception{
		return "manageProduct";
	}
	
	public String addProduct() throws Exception{
		resultMap = new HashMap<String, Object>();
		ProductService.addProduct(name, Product);
		resultMap.put("status", true);
		resultMap.put("msg", "保存成功");
		return SUCCESS;
	}
	
	public String delProduct() throws Exception{
		resultMap = new HashMap<String, Object>();
		
		// 删除关联外键
		Product product = productService.getProduct(id);
		Set<Customer> tmp = product.getCustomer();
		for (Customer c: tmp) {  
		      c.setProduct(null);
		      customerService.updateCustomer(c);
		}
		productService.delProduct(id);
		resultMap.put("status", true);
		resultMap.put("msg", "删除成功");
		return SUCCESS;
	}
	
	public String listProduct() throws Exception{
		List<Product> list = productService.listProduct();
		ActionContext ctx = ActionContext.getContext();
		ctx.put("list", list);
		return "listProduct";
	}

	public String listCustomer() throws Exception{
		Set<Customer> list = productService.getProduct(productId).getCustomer();
		ActionContext ctx = ActionContext.getContext();
		ctx.put("list", list);
		return "listCustomer";
	}
	
	// 前端接口
	public String outwardAdminNavigation() throws Exception{
		return "outwardAdminNavigation";
	}
	
	/*
	 * 列出 产品
	 * @param openId
	 */
	public String outwardListCustomer() throws Exception{
		ActionContext ctx = ActionContext.getContext();
		String openId = (String) ctx.get("openId");
		Set<Cusotmer> list = customerService.getcustomer(openId).getProduct().getCusotmer();
		ctx.put("list", list);
		return "outwardListCustomer";
	}
	
	/*
	 * 删除产品
	 * @param customerId
	 */
	public String delProductUser() throws Exception{
		ActionContext ctx = ActionContext.getContext();
		String openId = (String)ctx.get("openId");
		resultMap = new HashMap<String, Object>();
		
		Customer customer = customerService.getUser(Integer.parseInt(userId));
		if(customerService.getUser(openId).getProduct().equals(customer.getProduct())){
			customer.setProduct(null);
			customerService.updateCustomer(customer);
			resultMap.put("status", true);
			resultMap.put("ret", user.getName() + "已经被删除");
		} else{
			resultMap.put("status", false);
			resultMap.put("ret", "非法操作");
		}
		return SUCCESS;
	}
	
	/*
	 * 管理员跳转到产品信息页面
	 * @param openId
	 */
	public String outwardProductManage() throws Exception{
		ActionContext ctx = ActionContext.getContext();
		String openId = (String) ctx.get("openId");
		Product product = customerService.getCustomer(openId).getProduct();
		ctx.put("product", product);

		return "outwardProductManage";
	}
	
	/*
	 * 修改产品名称
	 * @param openId
	 */
	public String updateProductName() throws Exception{
		ActionContext ctx = ActionContext.getContext();
		resultMap = new HashMap<String, Object>();
		String openId = (String) ctx.get("openId");
		
		Product product = customerService.getCustomer(openId).getProduct();
		Product.setName(name);
		productService.updateProduct(product);
		
		resultMap.put("status", true);
		resultMap.put("ret", "修改成功啦");
		return SUCCESS;
	}
	
	public void setProductService(IProductService productService) {
		this.productService = productService;
	}

	public void setCustomeService(ICustomerService customerService) {
		this.customerService = customerService;
	}

	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSpecial() {
		return special;
	}
	public void setSpecial(String special) {
		this.special = special;
	}


	public String getProduct() {
		return product;
	}
	public void setProduct(String product) {
		this.products = products;
	}
	public Integer getProductsId() {
		return productsId;
	}

	public void setProductsId(Integer productsId) {
		this.productsId = productsId;
	}

	public String getCustomerId() {
		return customerId;
	}

	public void setCustomerId(String customerId) {
		this.customerId = customerId;
	}

	public HashMap<String, Object> getResultMap() {
		return resultMap;
	}

	public void setResultMap(HashMap<String, Object> resultMap) {
		this.resultMap = resultMap;
	}
}
